//
//  AddPersonViewController.m
//  17-HomeWorkContact
//
//  Created by 鹏飞罗 on 16/8/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AddPersonViewController.h"

@interface AddPersonViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UIImagePickerController *picker;
@end

@implementation AddPersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view from its nib.
    _personImage.backgroundColor = [UIColor redColor];
    _personImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_personImage addGestureRecognizer:tap];
   /** 用UIImagePickerController 类来获取图片视频，大体分为以下几个步骤：
    
    1. 初始化UIImagePickerController 类；
    
    2. 设置UIImagePickerController 实例的数据来源类型（下面解释）；
    
    3. 设置设置代理；
    
    4. 如果需要做图片修改的话设置allowsEditing =yes。
    */
  //1.初始化 UIImagePickerController
    _picker = [[UIImagePickerController alloc] init];
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
  //2.设置UIImagePickerController 实例的数据来源类型
    _picker.sourceType = sourceType;
//   3. 设置设置代理
    _picker.delegate = self;
//    4.如果需要做图片修改的话设置
//    _picker.allowsEditing = YES;
    
    
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
    [self presentViewController:_picker animated:YES completion:nil];
}
// 当用户选取完成时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    _personImage.image = image;
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}
// 用户点击取消时退出picker
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (IBAction)keepButtonClicked:(id)sender {
    
    NSString *name = _name.text;
    NSString *phoneNumber = _phoneNumber.text;
    NSString *address = _address.text;
    NSString *imageName = @"02";
    Person *person = [[Person alloc] initWithName:name imageName:imageName phoneNumber:phoneNumber address:address];
    
    [self.addPersonDelegate addPerson:person];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
